package thread;

public class ThreadState {
    public static void main(String[] args) throws InterruptedException {
        Pile pile = new Pile();
        Thread t = new Thread(pile);
        System.out.println("Create thread: " + t.getState());
        t.start();
        System.out.println("Start thread: " + t.getState());
        Thread.sleep(100);
        System.out.println("Wait for a moment: " + t.getState());
        Thread.sleep(1000);
        System.out.println("Wait forever: " + t.getState());
        pile.notifyNow();
        System.out.println("Wake up the thread: " + t.getState());
        Thread.sleep(1000);
        System.out.println("Terminate the thread: " + t.getState());
    }
}

class Pile implements Runnable {
    public synchronized void waitForAMoment() throws InterruptedException {
        wait(500);
    }

    public synchronized void waitForever() throws InterruptedException {
        wait();
    }

    public synchronized void notifyNow() throws InterruptedException {
        notify();
    }

    @Override
    public void run() {
        try {
            waitForAMoment();
            waitForever();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
